/*
 * Created on 6. sep.. 2006
 * Created by Tomas
 */
package placing;



import java.util.List;

import junit.framework.TestCase;

import org.junit.Before;
import org.junit.Test;

import people.Group;
import people.Person;
import util.Debug;


public class RoomManagerTest extends TestCase {
	private RoomManager mgr;
	private Room norway, sweden;
	private Person johan, jonas, chris, henning;
	private Group scandinavian;
	

	@Before
	public void setUp() throws Exception {
		mgr = new RoomManager();
		
		norway = new Room(47, "Norway");
		sweden = new Room(46, "Sweden");

		
		johan = new Person(1, "Johan", "Golden");
		jonas = new Person(2, "Jonas", "Gardell");
		chris = new Person(3, "Chris", "Langham");
		henning = new Person(4, "Henning", "Wehn");
		
		scandinavian = new Group("Scandinavians");
		scandinavian.addPerson(johan);
		scandinavian.addPerson(jonas);
		
		Debug.ON = false;
	}
	

	
	@Test
	public void testAddingRooms() {
		Debug.ON = true;
		
		assertTrue(mgr.isEmpty());
		
		try {
			mgr.addRoom(norway);
			mgr.addRoom(sweden);
		} catch (RoomException e1) {
			fail("Rooms Norway and Sweden already managed.");
		}
		try {
			mgr.addRoom();
		} catch (RoomException e) {
			fail("Could not add another Room to be managed.");
		}
		
		try {
			mgr.addRoom(norway);
			fail("Allowed re-adding of Norway");
		} catch (RoomException e) {
			// This should happen
		}
		
		try {
			mgr.addRoom(12);
		} catch (RoomException e) {
			fail("Could not add a room with 12 Places");
		}
		
		
		
		assertFalse(mgr.isEmpty());
	}
	
	@Test
	public void testGettingRooms() throws RoomException, PlaceNotFoundException,
	ReservationConflictException {
		assertTrue(mgr.isEmpty());
		
		mgr.addRoom(norway);
		mgr.addRoom(sweden);

		
		sweden.addPlace();
		
		assertEquals(norway, mgr.getRoom(47));
		assertFalse(mgr.getRoom(46) == norway);

		try {
			mgr.getRoom(50);
			fail("Erroneously found no.50");
		} catch (RoomException e) {
			// This should happen
		}
		
		assertEquals(sweden, mgr.getRoomWithFreePlace());
		
		sweden.reserveRoom(jonas);
		
		try {
			mgr.getRoomWithFreePlace(johan);
			fail("Found place for johan");
		} catch (PlaceNotFoundException e) {
			// This should happen
		}

		assertEquals(sweden,mgr.getRoomWithFreePlace(jonas));
		
		try {
			mgr.getFreePlace(chris);
			fail("Found a Place for Chris");
		} catch (PlaceNotFoundException e) {
			// This should happen
		}
		
		Place plJonas = mgr.getFreePlace(jonas);
		plJonas.assign(jonas);
		
		try {
			mgr.getRoomWithFreePlace();
			fail("Still places available");
		} catch (PlaceNotFoundException e) {
			// Should happen
		}
		
		printRooms();
		
	}
	
	@Test
	public void testReservations() {
//		Debug.ON = true;
		try {
			mgr.addRoom(12);
			mgr.addRoom(31);
			mgr.addRoom(15);
		} catch (RoomException e) {
			fail("Could not add rooms");
		}
		
		printRooms();
		
		try {
			mgr.reserveAnyPlaceInRoom(chris, mgr.getRoom(1));
			mgr.reserveAnyPlaceInRoom(scandinavian, mgr.getRoom(2));
			mgr.reserveRoom(henning, mgr.getRoom(2));
		} catch (PlaceNotFoundException e) {
			fail("Something bad with places");
		} catch (RoomException e) {
			fail("Something bad with rooms");
		}
		
		
		printRooms();
		
	}
	
	@Test
	public void testRoomRemoval() {
//		Debug.ON = true;
		try {
			mgr.addRoom(12);
			mgr.addRoom(31);
			mgr.addRoom(15);
		} catch (RoomException e) {
			fail("Could not add rooms");
		}
		
		printRooms();
		
		//should work
		try {
			mgr.removeRoom(mgr.getRoom(1));
		} catch (RoomException e) {
			fail("Could not remove room 1");
		}
		
		try {
			mgr.removeRoom(2);
		} catch (RoomException e) {
			fail("Could not remove room with number 2");
		}
		
		
		//should fail
		try {
			mgr.removeRoom(31);
			fail("Could remove already removed Room");
		} catch (RoomException e) {
			// Should happen
		}
		try {
			mgr.removeRoom(new Room(12));
			fail("Could remove already removed Room");
		} catch (RoomException e) {
			// Should happen
		}
	}



	private void printRooms() {
		if (Debug.ON) {
			List<Room> liRooms = mgr.getRooms();
			
			for (Room r : liRooms) {
				System.out.println(r);
			}
		}
	}

}
